/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.bo.PoiHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IPoiActionLocal;
import com.comut.crud.ejb.interfaces.remote.IPoiActionRemote;
import com.comut.crud.pojo.mobile.POIInfo;
import com.comut.crud.pojo.poi.Poi;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class PoiBean implements IPoiActionLocal, IPoiActionRemote {

    private static Logger _logger = Logger.getLogger(PoiBean.class.getName());

    @PostConstruct
    private void poiPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void poiPreDestroy() {
        _logger.info("PreDestroy");
    }

    public PoiBean() {
    }

    public Poi insertPoi(Poi poi) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        Poi toRet;
        PoiHandler th;
        try {
            th = new PoiHandler(connection);
            toRet = th.insertPoi(poi);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutPois();
        return toRet;
    }

    public void deletePoi(Poi poi) {
        if(poi.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        PoiHandler th;
        try {
            th = new PoiHandler(connection);
            th.deletePoi(poi);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutPois();
    }

    public void updatePoi(String poi) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        PoiHandler th;
        try {
            th = new PoiHandler(connection);
            th.updatePoi(poi);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutPois();
    }

     /**
     *  @author celia calcada
     *  ...
     */
    public Poi getPoi(Poi poi) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        PoiHandler th;
        Poi toRet = new Poi();
        try {
            th = new PoiHandler(connection);
            toRet = th.getPoi(poi);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }

    /**
     * Issue: IFL-1568
     * @author celia calcada
     * @param userId
     * @return
     */
    public POIInfo[] getPOIsForUserId(int userId) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        POIInfo[] ret = null;
        PoiHandler poiHandler;
        try {
            poiHandler = new PoiHandler(connection);
            ret = poiHandler.getPOIsForUserId(userId);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            poiHandler = null;
        }

        return ret;
    }
}
